const app = getApp()
import {
    showMsg
} from '../../../../utils/config.js'

import {
    validateEmoji,
    validateSpecial,
    validateMobile,
    validateMobile2
} from '../../../../utils/validate.js'
import {
    reqalarmReveiverEdit,
    reqSpaceReceiverList,
    reqSpaceReceiverAdd,
    reqSpaceReceiverBatchDel
} from '../../../../request/api'
Page({
    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        // parkId: '',
        currentTab: 0, //隔离中/解除隔离
        // 基本信息开始
        form: {}, //编辑存储信息
        receiveTypeList: [{
            id: 1,
            name: '公众号',
            active: false,
        }, {
            id: 2,
            name: '短信',
            active: false,
        }, {
            id: 0,
            name: '语音',
            active: false,
        }, ],
        // isEdit: false, //是否可编辑
        // 基本信息结束

        // 设备开始
        dataList: [{
            type: "烟感",
            batteryValue: null,
            classMark: "YL-IOT-YW03/B",
            closeDoor: 0,
            deviceId: "17053451865829060173008",
            deviceMac: "",
            deviceType: "SmokeAlarm",
            heartbeatTime: 24,
            id: "1769617435548758017",
            imei: "865829060173008",
            isAlarm: 1,
            manufacturerId: "1601149571463573599",
            manufacturerName: "源流科技",
            mark: "",
            online: 1,
            openDoor: 1,
            pic: "https://tagan.oss-cn-shenzhen.aliyuncs.com/2023/03/23/d9213de1b1b84b1780087f579ce9792fFrame 427319078@2x.png",
            productId: "1612276823406194689",
            productName: "烟感探测报警器",
            signalValue: null,
            spaceId: "1583020697800630273",
            spaceName: "c座写字楼-6层-103",
            status: 0,
            workMode: 1,
        }, {
            type: "烟感",
            batteryValue: null,
            classMark: "YL-IOT-YW03/B",
            closeDoor: 0,
            deviceId: "17053451865829060173008",
            deviceMac: "",
            deviceType: "SmokeAlarm",
            heartbeatTime: 24,
            id: "1769617435548758017",
            imei: "865829060173008",
            isAlarm: 1,
            manufacturerId: "1601149571463573599",
            manufacturerName: "源流科技",
            mark: "",
            online: 1,
            openDoor: 1,
            pic: "https://tagan.oss-cn-shenzhen.aliyuncs.com/2023/03/23/d9213de1b1b84b1780087f579ce9792fFrame 427319078@2x.png",
            productId: "1612276823406194689",
            productName: "烟感探测报警器",
            signalValue: null,
            spaceId: "1583020697800630273",
            spaceName: "c座写字楼-6层-103",
            status: 0,
            workMode: 1,
        }, {
            type: "烟感",
            batteryValue: null,
            classMark: "YL-IOT-YW03/B",
            closeDoor: 0,
            deviceId: "17053451865829060173008",
            deviceMac: "",
            deviceType: "SmokeAlarm",
            heartbeatTime: 24,
            id: "1769617435548758017",
            imei: "865829060173008",
            isAlarm: 1,
            manufacturerId: "1601149571463573599",
            manufacturerName: "源流科技",
            mark: "",
            online: 1,
            openDoor: 1,
            pic: "https://tagan.oss-cn-shenzhen.aliyuncs.com/2023/03/23/d9213de1b1b84b1780087f579ce9792fFrame 427319078@2x.png",
            productId: "1612276823406194689",
            productName: "烟感探测报警器",
            signalValue: null,
            spaceId: "1583020697800630273",
            spaceName: "c座写字楼-6层-103",
            status: 0,
            workMode: 1,
        },{
            type: "烟感",
            batteryValue: null,
            classMark: "YL-IOT-YW03/B",
            closeDoor: 0,
            deviceId: "17053451865829060173008",
            deviceMac: "",
            deviceType: "SmokeAlarm",
            heartbeatTime: 24,
            id: "1769617435548758017",
            imei: "865829060173008",
            isAlarm: 1,
            manufacturerId: "1601149571463573599",
            manufacturerName: "源流科技",
            mark: "",
            online: 1,
            openDoor: 1,
            pic: "https://tagan.oss-cn-shenzhen.aliyuncs.com/2023/03/23/d9213de1b1b84b1780087f579ce9792fFrame 427319078@2x.png",
            productId: "1612276823406194689",
            productName: "烟感探测报警器",
            signalValue: null,
            spaceId: "1583020697800630273",
            spaceName: "c座写字楼-6层-103",
            status: 0,
            workMode: 1,
        } ],
        activeName: '0',
        page: 1,
        searchValue: '',
        isNextPage: true,
        triggered: false,
        spaceArr: [], //批量删除时存储的spaceId集合
        // 设备结束
        disabledBtn: false,
        disabledBtn1: true,

        showlevel: false,
        levelList: [{
            id: 1,
            text: "一级"
        }, {
            id: 2,
            text: "二级"
        }, {
            id: 3,
            text: "三级"
        }, {
            id: 4,
            text: "四级"
        }],
        levelName: "一级",
        level: 1
    },
    onLoad(e) {
        // console.log(JSON.parse(e.item));
        // this.getItemInfo(e)
    },
    onShow() {
        // this.getSpaceReceiverList()
        // this.getBuildList()
    },
    clickShowSpace() {
        this.setData({
            showSpace: true
        })
    },
    // 点击关闭弹窗
    closeSpace() {
        this.setData({
            showSpace: false
        })
    },
    // 点击确认
    confirmSpace() {

    },
    onChange(event) {
        this.setData({
            activeName: event.detail,
        });
    },
    // 获取信息
    getItemInfo(e) {
        let item = JSON.parse(e.item)
        this.setData({
            // parkId: item.parkId,
            // 'form.parkId': item.parkId,
            'form.id': item.id,
            'form.name': item.name,
            'form.mobile': item.mobile,
            // 'receiveTypeList[0].active': item.mobileReceive === 1 ? true : false,
            // 'receiveTypeList[1].active': item.mpReceive === 1 ? true : false,
            // 'receiveTypeList[2].active': item.msgReceive === 1 ? true : false,
            'receiveTypeList[0].active': item.mpReceive === 1 ? true : false,
            'receiveTypeList[1].active': item.msgReceive === 1 ? true : false,
            'receiveTypeList[2].active': item.mobileReceive === 1 ? true : false,
        })
    },

    // 获取名字
    nameChange(e) {
        this.setData({
            'form.name': e.detail.value
        })
    },
    // 清除名字
    nameClear() {
        this.setData({
            'form.name': ''
        })
    },
    // 获取手机号
    mobileChange(e) {
        this.setData({
            'form.mobile': e.detail.value
        })
    },
    // 清除手机号
    mobileClear() {
        this.setData({
            'form.mobile': ''
        })
    },

    // 获取通知方式
    receiveTypeChange(e) {
        let {
            id
        } = e.currentTarget.dataset
        let {
            receiveTypeList
        } = this.data
        receiveTypeList.forEach(item => {
            if (item.id === id) {
                if (e.detail) {
                    item.active = true
                } else {
                    item.active = false
                }
            }
        })
        this.setData({
            receiveTypeList
        })
    },

    // 确认编辑
    confirm() {
        if (this.data.disabledBtn) return
        let {
            form,
            receiveTypeList
        } = this.data
        if (!form.name) return showMsg("请输入真实姓名")
        if (!form.mobile) return showMsg("请输入手机号码")
        if (!validateMobile2(form.mobile)) return
        let params = {
            orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
            id: form.id,
            spaceId: '',
            name: form.name,
            mobile: form.mobile,
            // mobileReceive: receiveTypeList[0].active ? 1 : 0,
            // mpReceive: receiveTypeList[1].active ? 1 : 0,
            // msgReceive: receiveTypeList[2].active ? 1 : 0,
            mpReceive: receiveTypeList[0].active ? 1 : 0,
            msgReceive: receiveTypeList[1].active ? 1 : 0,
            mobileReceive: receiveTypeList[2].active ? 1 : 0,
        }
        this.reqAddFun(params)
    },
    // 请求接口
    async reqAddFun(params) {
        const res = await reqalarmReveiverEdit(params)
        if (res.code != 20000) return showMsg(res.message);
        showMsg("编辑成功", "success")
        this.setData({
            disabledBtn: true
        })
        setTimeout(() => {
            wx.navigateBack({
                delta: 1,
            })
        }, 1000)
    },
    /**
     * @name: 滑动切换
     * @msg:  1、赋值curren
     * @param {*} e
     * @return {*}
     */
    bindChangeTab(e) {
        let {
            current
        } = e.detail
        this.setData({
            currentTab: current
        })
    },

    // 获取接警人空间分页
    async getSpaceReceiverList() {
        const res = await reqSpaceReceiverList(this.data.page, 20, {
            id: this.data.form.id
        })
        if (res.code !== 20000) return showMsg(res.message)
        let {
            rows
        } = res.data
        if (this.data.page === 1) {
            this.setData({
                dataList: rows,
                isNextPage: true
            })
        } else if (this.data.page > 1 && rows.length) {
            this.setData({
                dataList: this.data.dataList.concat(rows),
                isNextPage: true
            })
        } else if (this.data.page > 1 && !rows.length) {
            showMsg('已经到底啦~')
            this.setData({
                isNextPage: false
            })
        }
    },
    // 滚动到底部触发
    lower() {
        if (!this.data.isNextPage) return
        this.setData({
            page: this.data.page + 1,
        })
        this.getSpaceReceiverList()
    },
    // 下拉刷新
    async onRefresh() {
        this.setData({
            dataList: [],
            page: 1,
            isNextPage: true,
            spaceArr: [],
            triggered: true,
        })
        await this.getSpaceReceiverList()
        this.setData({
            triggered: false,
        })
    },
    // 操作列表后回到最初状态
    getInitSpaceReceiverList() {
        this.setData({
            page: 1,
            isNextPage: true,
            dataList: []
        })
        this.getSpaceReceiverList()
    },
    // 接警人增加空间接口函数
    async spaceReceiverAddFun(id) {
        let params = {
            id: this.data.form.id,
            spaceId: id,
            level: this.data.level
        }
        const res = await reqSpaceReceiverAdd(params)
        if (res.code !== 20000) return showMsg(res.message)
        this.setData({
            spaceArr: [],
        })
        wx.showLoading({
            mask: true,
            title: '加载中'
        })
        if (this.data.activeSpace == 'build') {
            setTimeout(() => {
                this.getInitSpaceReceiverList()
            }, 8000)
        } else if (this.data.activeSpace == 'floor') {
            setTimeout(() => {
                this.getInitSpaceReceiverList()
            }, 5000)
        } else if (this.data.activeSpace == 'room') {
            setTimeout(() => {
                this.getInitSpaceReceiverList()
            }, 1000)
        }

    },

    // 获取spaceArr的值
    spaceChange(event) {
        console.log(11);
        this.setData({
            spaceArr: event.detail
        })
        let {
            spaceArr
        } = this.data
        if (spaceArr.length > 0) {
            this.setData({
                disabledBtn1: false
            })
        } else {
            this.setData({
                disabledBtn1: true
            })
        }
    },
    // 点击获取spaceArr的值
    spaceToggle(event) {
        const {
            id
        } = event.currentTarget.dataset;
        this.checkboxChange(id)
    },
    // 复选框变化渲染
    checkboxChange(id) {
        const checkbox = this.selectComponent(`.checkboxes-${id}`);
        checkbox.toggle();
        console.log(this.data.spaceArr);
        let {
            spaceArr
        } = this.data
        if (spaceArr.length > 0) {
            this.setData({
                disabledBtn1: false
            })
        } else {
            this.setData({
                disabledBtn1: true
            })
        }
    },

    // 点击确定批量删除空间
    async confirmSpaceReceiverBatchDel() {
        if (this.data.disabledBtn1) return
        let {
            spaceArr
        } = this.data
        if (spaceArr.length <= 0) return showMsg("请选择要移除的安装点")
        if (spaceArr.length > 50) return showMsg("一次最多移除50个安装点")
        const res = await reqSpaceReceiverBatchDel({
            idList: spaceArr
        })
        if (res.code !== 20000) return showMsg(res.message)
        this.setData({
            spaceArr: [],
            disabledBtn1: true
        })
        this.getInitSpaceReceiverList()
    },

    // 确定监管等级
    levelConfirm(e) {
        console.log(e);
        this.setData({
            levelName: e.detail.value.text,
            level: e.detail.value.id,
            showlevel: false,
        })
    },
})